package coml.allen.api.rt;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

/**
 * @author sesshomaru
 * @date 2020/11/14 17:53
 */
public class Consumer {
    public static void main(String[] args) throws Exception{
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.1.102");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        String ex = "test_return_ex";
        String rk = "return.#";
        String queueName = "return_queue";
        String rkError = "abc.save";

        channel.exchangeDeclare(ex,"topic",true);
        channel.queueDeclare(queueName,true,false,false,null);
        channel.queueBind(queueName,ex,rk);

        // 创建消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        // 设定消费模式
        channel.basicConsume(queueName, true,queueingConsumer);

        while (true) {
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("消费端: " + msg);
        }
    }
}
